#include"RingQueue.hpp"

#include<unistd.h>

constexpr int CONSUMER_NUM = 4;
constexpr int PRODUCOTR_NUM = 2;

void* Consumer(void* arg) {
  RingQueue<int>* rq = reinterpret_cast<RingQueue<int>*>(arg);

  while(1) {
    sleep(1);
    int data;
    rq->Pop(data);
    printf("[ %lu ] consumer get data ::%d\n", pthread_self(), data);
  }
}

void* Productor(void* arg) {
  RingQueue<int>* rq = reinterpret_cast<RingQueue<int>*>(arg);

  int data = 0;
  while(1) {
    rq->Push(data);
    printf("[ %lu ] productor send ::%d\n", pthread_self(), data);
    ++data;
    data %= 100;
  }
}

int main() {
  
  RingQueue<int>* rq = new RingQueue<int>();

  pthread_t c[CONSUMER_NUM];
  pthread_t p[PRODUCOTR_NUM];

  for(int i = 0; i < CONSUMER_NUM; i++) {
    pthread_create(c + i, nullptr, Consumer, reinterpret_cast<void*>(rq));
  }
  for(int i = 0; i < PRODUCOTR_NUM; i++) {
    pthread_create(p + i, nullptr, Productor, reinterpret_cast<void*>(rq));
  }

  for(int i = 0;i < CONSUMER_NUM; i++) {
    pthread_join(c[i], nullptr);
  }

  for(int i = 0; i < PRODUCOTR_NUM; i++) {
    pthread_join(p[i], nullptr);
  }
  
  delete rq;
  return 0;
}
